home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_07 / barbu / str.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-17  |  1.8 KB  |  59 lines

  1. article\Listing2
  2.  
  3. //////////////////////////////////////////////////////
  4. //    STR.HPP    Null-Ended String Class
  5. //////////////////////////////////////////////////////
  6. #if !defined(STR_HPP)
  7. #define STR_HPP
  8. #include <string.h>
  9. class STR {
  10.     char *s;    // free store pointer
  11.     int  l;        // strlen + 1
  12.  
  13. public:
  14.     STR();                    // STR Dummy; l=1
  15.     STR(const STR&);
  16.     STR(const char szS[]);    // STR V("abc"); l=4
  17.     STR(int);                  // STR V(25); s[0]=0, l=26
  18.     ~STR();
  19.  
  20.     operator const char* () const { return s; }
  21.     int len() const { return strlen(s); }
  22.     int space() const { return l-1; }
  23.  
  24.     STR& operator=(const STR &y);
  25.     STR& operator+=(const STR &y);
  26.     int operator==(const STR &y) const
  27.             { return (0 == strcmp(s, y.s)); }
  28.     int operator!=(const STR &y) const
  29.             { return (0 != strcmp(s, y.s)); }
  30.  
  31.     STR& operator=(const char szS[]);
  32.     STR& operator+=(const char szS[]);
  33.     int operator==(const char szS[]) const
  34.             { return (0 == strcmp(s, szS)); }
  35.     int operator!=(const char szS[]) const
  36.             { return (0 != strcmp(s, szS)); }
  37.  
  38.     STR& operator=(char c);
  39.     STR& operator+=(char c);
  40.  
  41.     char& operator[](int i) { return s[i]; }
  42.     const char& operator[](int i) const
  43.             { return s[i]; }
  44.  
  45.     int streq(const char szS[]) const;
  46.     int hasin(const char szSubstring[],
  47.             int nFromIndex = 0, int bIgnoreCase = 0
  48.             ) const ;// returns absolute index or -1
  49.     int hasin(char c,
  50.             int nFromIndex = 0, int bIgnoreCase = 0
  51.             ) const ;// returns absolute index or -1
  52.  
  53.     STR& toupper() { strupr(s); return *this; }
  54.     STR& tolower() { strlwr(s); return *this; }
  55.     void noFrontSpace();    // "  \t\nabc" --> "abc"
  56.     void noTrailSpace();    // "abc  \t\n " --> "abc"
  57. };
  58. #endif
  59.